// pages/addManager/addManager.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    outside:{
      applyName:'',
      applyTel:'',
      orgPositions:'',
      structureList:[],
      isAdmin:1
    }
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  todepartment(){
    wx.navigateTo({
      url: '/pages/department/department?id=' + this.data.orgId
    })
  },
  nameIpt(e) {
    this.toggle('outside.applyName', e.detail.value)
  },
  phoneIpt(e) {
    this.toggle('outside.applyTel', e.detail.value)
  },
  phoneBlur(e){
    var uid = wx.getStorageSync('uid');
    var that = this;
    var phone = e.detail.value.replace(/ /g, '');
    if ( !(/^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|4|5|6|7|8|9][0-9]{9}))$/.test(phone))){
      wx.showToast({
        title: "手机号格式错误",
        icon: 'none',
        duration: 2000
      })
      return
    }
    app.req({
      url:"orgmini/org/newCheckWebOrgUser",
      method:"post",
      data:{
        uid: uid,
        orgId: that.data.orgId,
        applyTel: phone
      },
      success(res){
        console.log("手机号失焦结果",res)
        var text = '';
        if(res.code == 200){
          if(res.data.type == 1){
            text = '此用户已经是组织内成员';
          } else if (res.data.type == 2){
            text = '此用户已被管理员冻结，请在官网恢复后再试';
          }
          if (text){
            wx.showModal({
              title: '提示',
              content: text,
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  that.toggle('outside.applyTel', "")
                }
              }
            })
          }
        }
      }
    })
  },
  postIpt(e){
    this.toggle('outside.orgPositions', e.detail.value)
  },
  validate: function() {
    var that = this;
    var text = "";
    var name = that.data.outside.applyName;
    var phone = that.data.outside.applyTel.replace(/ /g, '');
    if (!name) {
      text = "姓名不能为空";
    } else if (name.length > 10) {
      text = "姓名长度不能大于10";
    } else if (!phone) {
      text = "手机号码不能为空";
    } else if (!(/^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|4|5|6|7|8|9][0-9]{9}))$/.test(phone))) {
      text = "手机号格式错误";
    }
    if (!!text) {
      wx.showToast({
        title: text,
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    return true;
  },
  toSave() {
    var that = this
    if (!that.validate()) {
      return;
    }else{
      var arr = []
      arr.push(that.data.outside)
        app.req({
          url: 'orgmini/structure/judgeOrgAdmin',
          data: {
            orgId: that.data.orgId,
            managerPhone: that.data.outside.applyTel.replace(/ /g, '')
          },
          success: function(res) {
            if (200 == res.code) {
              let pages = getCurrentPages(),
                prevPage = pages[pages.length - 2];
              prevPage.setData({
                orgOut: arr
              })
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none"
              })
            }
          }
        });
    }
    // app.req({
    //   url: 'orgmini/manager/admin//add',
    //   data: {
    //     orgId: that.data.orgId,
    //     manager: that.data.name,
    //     managerPhone: that.data.phone.replace(/ /g, '')
    //   },
    //   success: function(res) {
    //     if (200 == res.code) {
    //       wx.navigateBack()
    //     } else {
    //       wx.showToast({
    //         title: res.msg,
    //         icon: "none"
    //       })
    //     }
    //   }
    // });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      orgId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})